package com.game.module.skill
{
	import com.dusk.util.ResUtil;
	import com.dusk.util.TextUtil;
	import com.game.event.GameEventType;
	import com.game.manager.EventManager;
	import com.game.manager.GameSceneManager;
	import com.game.view.layer.LayerBase;
	
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	public class ManageSkill extends LayerBase
	{
		
		public function ManageSkill()
		{
			super();
		}
		
		public var lhTxt:TextField;
		public var closeBtn:SimpleButton;
		public var passiveBtn:SimpleButton;
		public var activeBtn:SimpleButton;
		public var skillPanel:LayerBase;
		
		override protected function beforeAdded():void
		{
			var mc:MovieClip = ResUtil.getLayerView("ManageSkill") as MovieClip;
			TextUtil.redrawTxt(mc);
			lhTxt = mc.getChildByName("lhTxt") as TextField;
			activeBtn = mc.getChildByName("activeBtn") as SimpleButton;
			passiveBtn = mc.getChildByName("passiveBtn") as SimpleButton;
			closeBtn = mc.getChildByName("closeBtn") as SimpleButton;
			setCloseBtn(closeBtn);
			addChild(mc);
		}
		
		public function refreshPanel():void
		{
			lhTxt.text = gc.playerData.lhValue.toString();
		}
		
		private function passiveClick(evt:MouseEvent):void
		{
			if (skillPanel)
			{
				skillPanel.removeFromParent();
			}
			skillPanel = new PassiveSkill(gc.playerData);
			addChild(skillPanel);
		}
		
		private function activeClick(evt:MouseEvent):void
		{
			if (skillPanel)
			{
				skillPanel.removeFromParent();
			}
			skillPanel = new ActiveSkill(gc.playerData);
			addChild(skillPanel);
		}
		
		override protected function onAdded():void
		{
			var roleName:MovieClip;
			switch (gc.playerData.roleId)
			{
				case 1:
					roleName = ResUtil.getComponentView("SelectWK") as MovieClip;
					break;
				case 2:
					roleName = ResUtil.getComponentView("SelectTS") as MovieClip;
					break;
				case 3:
					roleName = ResUtil.getComponentView("SelectBJ") as MovieClip;
					break;
				case 4:
					roleName = ResUtil.getComponentView("SelectSS") as MovieClip;
					break;
			}
			roleName.gotoAndStop(1);
			roleName.x = 50;
			roleName.y = 15;
			addChild(roleName);
			passiveBtn.addEventListener(MouseEvent.CLICK, passiveClick);
			activeBtn.addEventListener(MouseEvent.CLICK, activeClick);
			EventManager.getIns().registerListen(GameEventType.CLOSE_MANAGE_SKILL, removeFromParent);
			EventManager.getIns().dispatchEvent(GameEventType.PAUSE_GAME);
			activeBtn.dispatchEvent(new MouseEvent(MouseEvent.CLICK));
			refreshPanel();
		}
		
		override protected function onRemoved():void
		{
			activeBtn.removeEventListener(MouseEvent.CLICK, activeClick);
			passiveBtn.removeEventListener(MouseEvent.CLICK, passiveClick);
			if (!gc.isInBattle)
			{
				GameSceneManager.getIns().switchScene(GameSceneManager.GAME_MAP);
			}
			EventManager.getIns().dispatchEvent(GameEventType.CONTINUE_GAME);
			EventManager.getIns().dispatchEvent(GameEventType.REFRESH_SKILL);
		}
	}
}
